<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      table,
      td {
        border: 1px solid black;
        border-collapse: collapse;
      }
      table tr:nth-child(odd) {
        background-color: #aef;
      }
      table tr:nth-child(even) {
        background-color: #fcb;
      }
      td {
        width: 180px;
        height: 50px;
      }
    </style>
  </head>
  <body>
    <table>
      <tr>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
      </tr>
    </table>
  </body>
  <script>
    // 获取所有的td
    let tds = document.querySelectorAll("td");
    tds.forEach((td) => {
      td.onclick = function () {
        this.style.background = "red";
      };
    });
  </script>
</html>
